package com.eatme.web.sys.quartz.controller;

import com.eatme.web.app.coupon.common.exception.AppException;
import com.eatme.web.common.vo.AppResponse;
import com.eatme.web.sys.quartz.req.QuartzJobReq;
import com.eatme.web.sys.quartz.req.QuartzJobResp;
import com.eatme.web.sys.quartz.service.ISchedulerJobService;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 定时任务在线管理
 * @author nzc
 */
@RestController
@RequestMapping("sys/job")
public class QuartzJobController {
	private static final Logger LOG = LoggerFactory.getLogger(QuartzJobController.class);

	private final ISchedulerJobService schedulerJobServiceImpl;
	private final Scheduler scheduler;

	public QuartzJobController(ISchedulerJobService schedulerJobServiceImpl, Scheduler scheduler) {
		this.schedulerJobServiceImpl = schedulerJobServiceImpl;
		this.scheduler = scheduler;
	}


	/**
	 * 添加定时任务
	 * 
	 * @param quartzJobReq
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public AppResponse add(@RequestBody QuartzJobReq quartzJobReq) {
		String jobClassName = quartzJobReq.getJobClassName();
		String group = quartzJobReq.getGroup();
		String cronExpression = quartzJobReq.getCronExpression();
		String description = quartzJobReq.getDescription();
		LOG.info("创建定时任务:{}, {}, {}, {}",jobClassName,group,cronExpression,cronExpression,description);
//		QuartzJobReq db_QuartzJobReq = QuartzJobReqService.findByJobClassName(QuartzJobReq.getJobClassName());
//		if (db_QuartzJobReq!=null) {
//			return AppResponse.error("该定时任务类名已存在");
//		}
		schedulerJobServiceImpl.addAndschedulerJob(quartzJobReq);
		AppResponse response = AppResponse.ok("创建job并调度成功");
		LOG.info("创建定时任务结束：{}", response);
		return response;
	}

	/**
	 * 暂停定时任务
	 * 
	 * @param quartzJobReq
	 * @return
	 */
	@PostMapping(value = "/pause")
	public AppResponse<Object> pauseJob(@RequestBody QuartzJobReq quartzJobReq) throws AppException {
		LOG.info("暂停定时任务:{}, {}",quartzJobReq.getJobClassName(),quartzJobReq.getGroup());
		try {

			scheduler.pauseJob(JobKey.jobKey(quartzJobReq.getJobClassName().trim(),quartzJobReq.getGroup()));
		} catch (SchedulerException e) {
			throw new AppException("暂停定时任务失败");
		}
		schedulerJobServiceImpl.pauseJob(quartzJobReq.getJobClassName().trim(),quartzJobReq.getGroup());
		AppResponse response = AppResponse.ok("重置job成功");
		LOG.info("暂停job结束：{}", response);
		return response;
	}

	/**
	 * 恢复定时任务
	 * 
	 * @param job
	 * @return
	 */
	@PostMapping(value = "/resume")
	public AppResponse<Object> resumeJob(@RequestBody QuartzJobReq job) {
		LOG.info("重启定时任务开始:{}, {}",job.getJobClassName(),job.getGroup());
		schedulerJobServiceImpl.resumeJob(job);
		AppResponse response = AppResponse.ok("重置job成功");
		LOG.info("重启定时任务结束：{}", response);
		return response;
	}
	/**
	 * 重置定时任务
	 *
	 * @param job
	 * @return
	 */
	@PostMapping(value = "/reset")
	public AppResponse<Object> resetJob(@RequestBody QuartzJobReq job)  {
		// String jobClassName = QuartzJobReq.getJobClassName();
		String group = job.getGroup();
		String cronExpression = job.getCronExpression();
		String description = job.getDescription();
		LOG.info("创建定时任务:{}, {}, {}, {}",job.getJobClassName(),group,cronExpression,cronExpression,description);
		schedulerJobServiceImpl.resetJob(job);
		AppResponse response = AppResponse.ok("重置job成功");
		LOG.info("更新定时任务结束：{}", response);
		return response;
	}

	/**
	 * 通过id删除
	 *
	 * @param job
	 * @return
	 */
	@RequestMapping(value = "/delete")
	public AppResponse delete(@RequestBody QuartzJobReq job) {
		LOG.info("删除定时任务开始：{}，{}", job.getJobClassName(), job.getGroup());
		schedulerJobServiceImpl.deleteJob(job);
		AppResponse response = AppResponse.ok("删除job成功!");
		LOG.info("删除定时任务结束：{}", response);
		return response;

	}

	@RequestMapping(value = "/trigger")
	public AppResponse trigger(@RequestBody QuartzJobReq job) throws SchedulerException {
		String jobClassName = job.getJobClassName();
		String jobGroupName = job.getGroup();
		LOG.info("手动执行任务开始：{}, {}", jobClassName, jobGroupName);
		schedulerJobServiceImpl.triggerJob(jobClassName, jobGroupName);
		return AppResponse.ok();
	}

	@RequestMapping(value="/query")
	public AppResponse query() {
		LOG.info("查看所有定时任务开始");
		List<QuartzJobResp> list =  schedulerJobServiceImpl.queryJob();
		AppResponse response = AppResponse.ok().data(list);
		LOG.info("查看定时任务结束：{}", response);
		return response;
	}

}
